package org.gw.ylc.base.repositories;

import org.gw.ylc.base.model.Administrator;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

public interface AdministratorRepository extends CrudRepository<Administrator, String> {

	public Administrator findByEmployeeNoAndApp(String userId, String app);

	@Query("from Administrator a where (?1 is null or a.employeeNo like ?1 or a.name like ?1) "
			+ "and (?2 is null or a.manaDep = ?2) and (?3 is null or a.type = ?3 and a.type != 4) and (?4 is null or a.app=?4)")
	public Page<Administrator> findByNameAndDepartmentAndType(String name, String department, Integer type, String app,
                                                              Pageable psageable);

	@Query("delete from Administrator a where a.id=?1 and a.type!=3 and (?2 is null or a.app=?2)")
	@Modifying(clearAutomatically = true)
	public void deleteNormalAdmin(String id, String app);
	
	public Administrator findById(String id);

	@Modifying(clearAutomatically = true)
	@Query("delete from Administrator a where a.employeeNo = ?1 and a.type = 4 and (?2 is null or a.app=?2)")
	void deleteBranchAdmin(String xh, String app);

}
